#include<iostream>
#include<functional>
#include<vector>
#include<algorithm>
#include<time.h>

using namespace std;

template<typename Container>
void showContainer(Container &con){
    //由于编译的原因，所以需要加上typename，防止无法识别Container类型
    typename Container::iterator it = con.begin();
    for (; it != con.end();it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}
template <typename Compare, typename T>
class _mybind1st{
    public:
    _mybind1st(Compare comp,T val):
        _comp(comp),_val(val){}

    bool operator()(const T &second){
        return _comp(_val, second);
    }
    private:
        Compare _comp;
        T _val;
};

template <typename Compare, typename T>
_mybind1st<Compare, T> mybind1st(Compare comp, const T &val)
{
    return _mybind1st<Compare, T>(comp, val);
}

template<typename Iterator,typename Compare>
Iterator my_find_if(Iterator first,Iterator last,Compare comp){
    for (; first != last;first++){
        //comp.operator()(*first)
        if(comp(*first)){
            return first;
        }
    }
    return last;
}

int main(){
    vector<int> vec;
    srand(time(nullptr));
    for (int i = 0; i < 20;i++)
    {
        //产生1~100之间的随机数
        vec.push_back(rand() % 100 + 1);
    }

    showContainer(vec);
    sort(vec.begin(), vec.end());
    showContainer(vec);

    // greater二元函数对象，用于从大到小排序
    sort(vec.begin(), vec.end(), greater<int>());
    showContainer(vec);

    // bind1st和bind2nd可以将二元函数对象转化为一元函数对象，也就是说传入的默认值对应二元函数对象的形参
    // 寻找第一个小于70的索引，原序列按照从大到小排序 return 70>*it 
    // auto it = find_if(vec.begin(), vec.end(), bind1st(greater<int>(), 70));
    // 寻找第一个小于70的索引，原序列按照从大到小排序 return *it<70
    // auto it = find_if(vec.begin(), vec.end(), bind2nd(less<int>(), 70));

    auto it = my_find_if(vec.begin(), vec.end(), mybind1st(greater<int>(), 70));
    // auto it = my_find_if(vec.begin(), vec.end(), bind2nd(less<int>(), 70));

    if(it!=vec.end())
    {
        vec.insert(it, 70);
    }
    showContainer(vec);
    return 0;
}

